package com.atguigu.educms.controller;


import com.atguigu.commonutils.R;
import com.atguigu.educms.entity.CrmBanner;
import com.atguigu.educms.service.CrmBannerService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author shy
 * @since 2021-06-14
 */
@RestController
@CrossOrigin
@RequestMapping("/cmsservice/bannerAdmin")
public class BannerAdminController {
    @Autowired
    private CrmBannerService crmBannerService;

    @PostMapping("/pageBanner/{page}/{limit}")
    public R pageBanner(@PathVariable Long page, @PathVariable Long limit){
        Page<CrmBanner> bannerPage = new Page<>(page, limit);
        crmBannerService.page(bannerPage, null);
        return R.ok().data("items", bannerPage.getRecords()).data("total", bannerPage.getRecords());
    }

    @PostMapping("/addBanner")
    public R addBanner(@RequestBody CrmBanner crmBanner){
        boolean flag = crmBannerService.save(crmBanner);
        if (flag){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @DeleteMapping("/deleteBannerById/{id}")
    public R deleteBannerById(@PathVariable String id ){
        boolean flag = crmBannerService.removeById(id);
        if (flag){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @PostMapping("/updateBanner")
    public R updateBanner(@RequestBody CrmBanner crmBanner ){
        boolean flag = crmBannerService.updateById(crmBanner);
        if (flag){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @GetMapping("/getBannerById/{id}")
    public R updateBanner(@PathVariable String id ){
        CrmBanner crmBanner = crmBannerService.getById(id);
        return R.ok().data("crmBanner", crmBanner);
    }

}

